package com.blacktry.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blacktry.entity.model.Classroom;
import com.blacktry.entity.model.CourseDistribution;
import com.blacktry.mapper.ClassroomMapper;
import com.blacktry.service.ClassroomService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blacktry.service.CourseDistributionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author blackTry
 * @since 2021-12-06
 */
@Service
public class ClassroomServiceImpl extends ServiceImpl<ClassroomMapper, Classroom> implements ClassroomService {

    @Override
    public int updateClassroom(Classroom classroom) {
        return this.baseMapper.updateById(classroom);
    }

    @Override
    public Map<Integer, List<Classroom>> selectParam(Classroom classroom, Page<Classroom> page) {
        Map<Integer, List<Classroom>> map = new HashMap<>();

        QueryWrapper<Classroom> wrapper1 = new QueryWrapper<>();
        wrapper1.lambda().eq(Classroom::getType, 1);
        wrapper1.orderByDesc("num");
        List<Classroom> list1 = this.baseMapper.selectPage(page, wrapper1).getRecords();
        map.put(1, list1);

        QueryWrapper<Classroom> wrapper2 = new QueryWrapper<>();
        wrapper2.lambda().eq(Classroom::getType, 2);
        wrapper2.orderByDesc("num");
        List<Classroom> list2 = this.baseMapper.selectPage(page, wrapper2).getRecords();
        map.put(2, list2);

        QueryWrapper<Classroom> wrapper3 = new QueryWrapper<>();
        wrapper3.lambda().eq(Classroom::getType, 3);
        wrapper3.orderByDesc("num");
        List<Classroom> list3 = this.baseMapper.selectPage(page, wrapper3).getRecords();
        map.put(3, list3);

        QueryWrapper<Classroom> wrapper4 = new QueryWrapper<>();
        wrapper4.lambda().eq(Classroom::getType, 4);
        wrapper4.orderByDesc("num");
        List<Classroom> list4 = this.baseMapper.selectPage(page, wrapper4).getRecords();
        map.put(4, list4);

        return map;
    }

    @Autowired
    CourseDistributionService courseDistributionService;
    @Override
    public Boolean selectClassroom(Integer id, Integer classroom_id) {

        CourseDistribution courseDistribution = courseDistributionService.getById(id);
        Integer logic_time = courseDistribution.getLogicTime();
        QueryWrapper<CourseDistribution> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(CourseDistribution::getClassroomId,classroom_id);
        List<CourseDistribution> list = courseDistributionService.getBaseMapper().selectList(wrapper);
        boolean anyMatch = list.stream().anyMatch(x->x.getLogicTime()==logic_time);
        return !anyMatch;
    }
}